package viz_ui;

import java.awt.Color;

import processing.core.PApplet;
import processing.core.PFont;
import utilities.Utils;
import buttons.view.V_VerticalDoubleSlider;

public class ParallelAxisSlider extends V_VerticalDoubleSlider {

	private boolean showSliders = false;
	private int sliderColor;
	private PFont B_Font;
	private String defaultFontName = "verdana-10";
	private int fontSize = 10;
	private float topValue;
	private float bottomValue;
	private final int rnd = 100;
	
	public ParallelAxisSlider(String n, float xWidth, float yHeight) {
		super(n, xWidth, yHeight);
		sliderColor = Utils.intColor(Color.orange);
		rectWidth = 10;
	}
	
	@Override
	public void display(PApplet p)
	{
		if(visible)
	    {  
			// draw the main plot line:
			p.strokeWeight(1.3f);
			p.stroke(0,0,0, transp);
			p.line(xLoc, yLoc, xLoc, yLoc+ySize);
			p.strokeWeight(1.3f);
			if(showSliders)
			{
				p.noFill();
				// calculate mouseOver colors:
				int tmpColor1 =  p.color(p.red(sliderColor)-r1_ColorOffset, p.green(sliderColor)-r1_ColorOffset, p.blue(sliderColor)-r1_ColorOffset, p.alpha(sliderColor));
				int tmpColor2 = p.color(p.red(sliderColor)-r2_ColorOffset, p.green(sliderColor)-r2_ColorOffset, p.blue(sliderColor)-r2_ColorOffset, p.alpha(sliderColor));
				int tmpColor3 = p.color(p.red(sliderColor)-r3_ColorOffset, p.green(sliderColor)-r3_ColorOffset, p.blue(sliderColor)-r3_ColorOffset, p.alpha(sliderColor));
				
				// draw the filter handles:
				// TODO: Convert to triangles
				p.stroke(tmpColor1);
				p.rect(xLoc-(xSize/2), yLoc+rect1, xSize, rectWidth);
				p.stroke(tmpColor2);
				p.rect(xLoc-(xSize/2), yLoc+rect2-rectWidth, xSize, rectWidth);
				
				// draw the middle percentage bar:
				p.stroke(tmpColor3);
			    //p.fill(240-r3_ColorOffset, 240-r3_ColorOffset, 240-r3_ColorOffset, transp);
			  	p.rect(xLoc, yLoc+rect1+rectWidth, 0.5f, rect2-rectWidth-(rect1+rectWidth));
			}
			
			// draw textLabels:
		  	if(B_Font != null)
		  	{
			  	p.fill(0,0,0);
			  	p.textFont(B_Font);
			  	p.text(name, xLoc - (p.textWidth(name)/2), yLoc-18);
			  	String tmpTop = (float)((int)(topValue*rnd))/rnd+"";
			  	String tmpBottom = (float)((int)(bottomValue*rnd))/rnd+"";
			  	p.text(tmpTop, xLoc-(p.textWidth(tmpTop)/2), yLoc - 4);
			  	p.text(tmpBottom, xLoc-(p.textWidth(tmpTop)/2), yLoc + ySize + fontSize + 4);
		  	}
			
			p.strokeWeight(1);
	    }
	}
	
	public void setShowSliders(boolean val)
	{
		showSliders = val;
	}
	
	// enable the use of the font by passing in the PApplet:
	public void useFont(PApplet p){
		B_Font = p.createFont(defaultFontName, fontSize);
	}
	
	public void setRange(float top, float bottom)
	{
		topValue = top;
		bottomValue = bottom;
	}
	
	@Override
	public int containsWhat(float xVal, float yVal)
	{
	    // check if mouse is over the first rectangle:
	    if (yVal < yLoc + rect1 + rectWidth && yVal > yLoc + rect1
	      && xVal < xLoc + xSize/2 && xVal > xLoc - xSize/2)
	    {
	      return 0;
	    }
	    // check if mouse is over the second rectangle:
	    else if (yVal < yLoc + rect2 && yVal > yLoc+rect2-rectWidth
	      && xVal < xLoc + xSize/2 && xVal > xLoc - xSize/2)
	    {
	      return 1;
	    }
	    // check if mouse is over the big rectangle:
	    else if (yVal < yLoc + rect2-rectWidth && yVal > yLoc+rect1+rectWidth
	      && xVal < xLoc + xSize/2 && xVal > xLoc - xSize/2)
	    {
	      return 2;
	    }
	    
	    else
	    {
	      return -1;
	    }
	}

}
